package com.zhoug.router3demo.ui;

import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import com.zhoug.json3.JsonUtils;
import com.zhoug.logger3.Logger;
import com.zhoug.router3.Router;
import com.zhoug.router3demo.IUserServer;
import com.zhoug.router3demo.Paths;
import com.zhoug.router3demo.R;

public class MainActivity extends MyBaseActivity {

    private Button mBtnActivity2;
    private Button mBtnFragment1;
    private Button mBtnErrorPath;
    private Button mBtnFirmUserServer;
    private Button mBtnUserHomeActivity;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void findViews() {

        mBtnActivity2 = findViewById(R.id.btn_Activity2);
        mBtnFragment1 = findViewById(R.id.btn_Fragment1);
        mBtnErrorPath = findViewById(R.id.btn_errorPath);
        mBtnFirmUserServer = findViewById(R.id.btn_firmUserServer);
        mBtnUserHomeActivity = findViewById(R.id.btn_UserHomeActivity);
    }

    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        mBtnErrorPath.setOnClickListener(v -> Router.getARouter().build("/app/testPathHHH").navigation());
        mBtnActivity2.setOnClickListener(v -> {
            Router.getARouter().build(Paths.Activity2).navigation();
        });
        mBtnUserHomeActivity.setOnClickListener(v -> {
            Router.getARouter().build(Paths.UserHomeActivity).withString("name", "张三").navigation();
        });
        mBtnFragment1.setOnClickListener(v -> {
            Object navigation = Router.getARouter().build(Paths.Fragment1).navigation();
            Toast.makeText(this, navigation.toString(), Toast.LENGTH_LONG).show();
        });
        //获取服务
        mBtnFirmUserServer.setOnClickListener(v -> {
            IUserServer userServer = Router.getProvider(IUserServer.class);
            print(userServer);
            userServer = Router.getProvider(Paths.AdminUserServer, IUserServer.class);
            print(userServer);
        });

    }


    private void print(IUserServer userServer) {
        if (userServer != null) {
            Logger.d(TAG, "user:" + JsonUtils.toJson(userServer.getUser()));
        } else {
            Logger.e(TAG, "userServer is null");
        }
    }
}